package fourEightDays;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2024-02-28
 * Time: 22:48
 */
public class Day2 {

    public static  void reverse(char[] arr,int start,int end) {
        while (start < end) {
            char tmp = arr[start];
            arr[start] = arr[end];
            arr[end] = tmp;
            start ++;
            end --;
        }
    }
    /**
     * 逆置字符串（先整体后单词）
     * @param args
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            char[] str = scanner.nextLine().toCharArray();
            // 1. 整体
            reverse(str,0, str.length-1);
            // 2. 局部单词
            int start = 0;
            int end = 0;
            while (end < str.length) {
                // 找到单词分界点
                while (end < str.length && str[end] != ' ') {
                    end ++;
                }
                // 反转单词
                if (end < str.length) {
                    reverse(str,start,end-1);
                    end = end + 1;
                    start = end;
                } else {
                    reverse(str,start,end-1);
                    end = end + 1;
                }
            }
            System.out.println(Arrays.toString(str));
        }
    }

    /**
     * 最少排序子序列
     * 思路：三种状态：a<b,a=b,a>b
     * @param args
     */
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextInt()) {
            // 个数
            int n = scanner.nextInt();
            // 接收(防止越界/遗漏，使用n+1个)
            int[] arr = new int[n+1];
            for (int i = 0; i < n; i++) {
                arr[i] = scanner.nextInt();
            }
            // 定义结果个数
            int res = 0;
            // 遍历判断
            // [1,2,1,2,1,2,1,2,1]
            int i = 0;
            while(i < n) {
                if (arr[i] < arr[i+1]) {
                    while (i + 1 < n && arr[i] <= arr[i+1]) {
                        i ++;
                    }
                    i ++;
                    res ++;
                } else if (arr[i] > arr[i+1]){
                    while (i + 1 < n && arr[i] >= arr[i+1]) {
                        i ++;
                    }
                    i ++;
                    res ++;
                } else {
                    i ++;
                }
            }
            // 返回结果
            System.out.println(res);
        }


    }
}
